package com.ljkj.cordial.lengthtcpheartbeat;

import android.text.TextUtils;

import com.google.protobuf.ByteString;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

public class ByteStringUtil {
    private void test() {
        String path = "/storage/emulated/0/com.ljkj.cordial.chat/65432187/AlbumImageCache/20191122_221403410_c6d7a6f83bffb6cea06db6a6628d6b10.jpg";
        byte[] by = ByteStringUtil.File2byte(new File(path));
        ByteString bs = ByteString.copyFrom(by);
        bs.toByteArray();
    }

    public static String bytesToString(ByteString src, String charSet) {
        if (TextUtils.isEmpty(charSet)) {
            charSet = "GB2312";
        }
        return bytesToString(src.toByteArray(), charSet);
    }

    public static String bytesToString(byte[] input, String charSet) {
        if (input == null || input.length == 0) {
            return null;
        }

        ByteBuffer buffer = ByteBuffer.allocate(input.length);
        buffer.put(input);
        buffer.flip();

        Charset charset = null;
        CharsetDecoder decoder = null;
        CharBuffer charBuffer = null;
        try {
            charset = Charset.forName(charSet);
            decoder = charset.newDecoder();
            charBuffer = decoder.decode(buffer.asReadOnlyBuffer());

            return charBuffer.toString();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static byte[] File2byte(File tradeFile) {
        byte[] buffer = null;
        try {
            FileInputStream fis = new FileInputStream(tradeFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

    public static void getFileByBytes(byte[] bytes, String filePath) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            file = new File(filePath);

            //输出流
            fos = new FileOutputStream(file);

            //缓冲流
            bos = new BufferedOutputStream(fos);

            //将字节数组写出
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String getNewFilePath(String path, ByteBuffer buffer) {

        try {
            File file = new File(path);
            if (!file.exists()) {
                file.createNewFile();
            }
            RandomAccessFile write = new RandomAccessFile(file, "rw");
            FileChannel writeChannel = write.getChannel();
            writeChannel.write(buffer);
            writeChannel.close();
            writeChannel = null;
            write.close();
            write = null;
            return file.getAbsolutePath();
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * @param code "\\346\\227\\240\\350\\275\\246\\347\\211\\214";
     * @return
     */
    public static String getChinese(String code) {

        //1.获取八进制数据
        String[] split = code.substring(1).split("\\\\");

        StringBuffer s16 = new StringBuffer();

        //2.把八进制转换成格式为:%+十六进制
        for (String s : split) {
            s16.append("%" + Integer.toHexString(Integer.valueOf(s, 8)).toString().toUpperCase());
        }

        //3.解码
        try {
            return URLDecoder.decode(s16.toString(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return code;

    }
}
